/*!
 * 提示信息(jtip) JavaScript Library v0.0.1
 *
 * Copyright (c) 2009 Zhejiang Innovation Central Software Co., Ltd.
 *
 * Autor: flys
 * Date: 2009-03-14
 */
(function($) {
	function Tiper() {
		this.maxWidth = 600;
		this.minWidth = 40;
		this.margin   = 10; //左右边距宽度
		this.padding  = 8;  //箭头深度
		this.depth = 1000;

		this.tipObj = null; //提示对象

		this.controllerTemplate = "<div id=\"popupController\"></div>"; 
		this.containerTemplate = "<div id=\"popContainer\"></div>";
		this.panelTemplate = "<div id=\"popPanel\">" +
					         "	    <div class=\"popLeft\"></div>" +
					         "	    <div id=\"popContainerText\"></div>" +
					         "	    <div class=\"popRight\"></div>" +
					         "</div>";
		this.arrowTemplate = "<div class=\"popArrow\"><span></span></div>";
		this.ieframeTemplate = "<iframe class=\"popupController_ieframe\" frameborder=\"1\" scrolling=no src=\"#\"></iframe>";

		this.show = function(obj, msg) {
			
			var _obj =  (typeof(obj) == 'object' ? obj : document.getElementById(obj));

			// obj如果不可见。设置弹出对象为obj父元素
			var $obj = $(_obj);
			if($obj.css("display") == "none" || $obj.css("visibility") == "hidden" 
					|| $obj.attr("type") == "hidden") {
				_obj = _obj.parentNode;
			}

			this.tipObj = _obj;

			this._setMessage(msg);

			this.rootLayers.css("display", "block");
//alert(this.popupController.outerHeight() + "@Container:" + this.popupContainer.outerHeight() + "@Panel:" + this.popupPanel.outerHeight() + "@arrow:" + this.arrow.outerHeight());

			if ($.browser.msie) {
				var panelWidth = this.popupPanel.outerWidth() + this.margin; //此处的10是左边和右边和宽度之和
				var panelHeight = this.popupContainer.outerHeight();
				
				//alert(panelWidth);
				this.popupController.css("width", panelWidth);

				if (this.ieframe) {
					this.ieframe.css({
					  width: panelWidth,
					  height: panelHeight
					});
				}
			}

			this._setPosition(_obj);

			//alert(document.all.popupController.outerHTML);
			
		}

		this.hide = function(ele) {
			var _element = ele;

			if (this._isNull(this.tipObj) || this._isNull(_element)) return;

			if ("string" == typeof(_element)) alert();
			
			if (ele == this.tipObj)
			this.rootLayers.css("display", "none");
		}

		this._clean = function() {
			if (this._isNull(this.tipObj)) return;
			this.rootLayers.css("display", "none");
		}

		this._isNull = function(o){
			return (o==null||"undefined"==typeof(o)) ? true : false;
		}

		this._setMessage = function(msg) {
			//alert(obj.id + "#"+ msg);
			this.popupText.text(msg);

			var textWidth = this.popupText.outerWidth();
			if (textWidth < this.minWidth - this.padding) {
				//this.popupText.css("width", this.minWidth - this.padding);
			}
		}

		this._setPosition = function(obj) {
			var offset = $(obj).offset();
			
			//alert(offset.left + "@" + offset.top + "@" + (offset.top - this.popupController.outerHeight()));
			this.popupController.css({
				left : offset.left,
				top: offset.top - this.popupController.outerHeight() + this.padding
			 });
		}

		this._init = function() {// 初始化

			this.popupController = this.rootLayers =  $(this.controllerTemplate).appendTo($(document.body))
				.css({display: "none", position: "absolute", zIndex: ++this.depth });

			this.popupContainer = $(this.containerTemplate).appendTo(this.popupController);
				//.css({position: "absolute", zIndex: this.depth });
			var $this = this;
			this.popupPanel = $(this.panelTemplate).appendTo(this.popupContainer).bind("dblclick", function() {
				$this._clean();
			});

			this.popupText = $("#popContainerText");
			
			this.arrow = $(this.arrowTemplate).appendTo(this.popupContainer);

			if ($.browser.msie && $.browser.version < 7) {
				this.ieframe = $(this.ieframeTemplate).css({ position: "absolute", zIndex: - 1, filter: "Alpha(style=0,opacity=0)"}).insertBefore(this.popupContainer);
			};

			//alert(this.popupController.html());

			//$("#popContainer").css("display", "none");
		}

		this._init();
	}

	$.ui = {
		$tiper: null, //提示框的实例，一般是单实例

　　		tip: function(single){

			var _single = (single == null) ? true : single;

			var _instance = $.ui.$tiper;

			if (_instance == null) {
				_instance = new Tiper();

				if (_single)  $.ui.$tiper = _instance;
			}
			return _instance;
		},

		xwindow: function() {
			alert("打开新窗口");
		}

	};

	jtip = $.tip = function(type) {
		return $.ui.tip(type);
	}

})(jQuery); 
    
